<template>
  <view class="login-page">
    <!-- 背景装饰 -->
    <view class="bg-decoration">
      <view class="bg-circle bg-circle-1"></view>
      <view class="bg-circle bg-circle-2"></view>
      <view class="bg-circle bg-circle-3"></view>
    </view>
    
    <!-- 自定义导航栏 -->
    <u-navbar 
      :is-back="true" 
      title="登录" 
      title-color="#fff"
      bg-color="rgba(255,255,255,0.9)"
      :border-bottom="false"
    />
    
    <!-- 页面内容 -->
    <view class="login-container">
      <!-- Logo区域 -->
      <view class="logo-section">
        <image class="logo" src="/static/logo.png" mode="aspectFit" />
        <text class="app-name">每日记</text>
        <text class="app-slogan">记录生活，珍藏美好</text>
      </view>
      
      <!-- 登录表单 -->
      <view class="login-form">
        <!-- 登录方式切换 -->
        <!-- <view class="login-type-tabs">
          <view 
            class="tab-item" 
            :class="{ active: loginType === 'username' }"
            @click="switchLoginType(0)"
          >
            账号登录
          </view>
          <view 
            class="tab-item" 
            :class="{ active: loginType === 'phone' }"
            @click="switchLoginType(1)"
          >
            手机登录
          </view>
        </view> -->
        
        <!-- 账号输入框 -->
        <view class="input-group">
          <view class="input-label">
            <text>用户名</text>
          </view>
          <view class="input-wrapper">
            <input
              v-model="formData.username"
              placeholder="请输入用户名"
              class="input-field"
              placeholder-class="input-placeholder"
            />
          </view>
        </view>
        
        <!-- 密码输入框 -->
        <view class="input-group">
          <view class="input-label">
            <text>密码</text>
          </view>
          <view class="input-wrapper">
            <input
              v-model="formData.password"
              placeholder="请输入密码"
              :password="!showPassword"
              class="input-field"
              placeholder-class="input-placeholder"
            />
            <view class="password-toggle" @click="togglePassword">
              <text>{{ showPassword ? '隐藏' : '显示' }}</text>
            </view>
          </view>
        </view>
        
        <!-- 记住密码选项 -->
        <view class="remember-section">
          <view class="remember-item" @click="toggleRemember">
            <view class="checkbox" :class="{ checked: rememberMe }">
              <text v-if="rememberMe" class="checkbox-mark">✓</text>
            </view>
            <text class="remember-text">记住密码</text>
          </view>
          <text class="forgot-password" @click="goToForgotPassword">忘记密码？</text>
        </view>
        
        <!-- 登录按钮 -->
        <view class="login-button-wrapper">
          <button 
            class="login-btn"
            :class="{ disabled: !canLogin }"
            :disabled="!canLogin || loading"
            @click="handleLogin"
          >
            <text v-if="loading">登录中...</text>
            <text v-else>登录</text>
          </button>
        </view>
        
        <!-- 注册链接 -->
        <view class="register-link">
          <text class="link-text">还没有账号？</text>
          <text class="link-btn" @click="goToRegister">立即注册</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { useUserStore } from '@/store/user'
import { loginAPI, wechatLoginAPI } from '@/api/auth'
import { isValidEmail, isValidPhone } from '@/utils/index'

export default {
  name: 'LoginPage',
  setup() {
    const userStore = useUserStore()
    return {
      userStore
    }
  },
  data() {
    return {
      formData: {
        username: '',
        password: ''
      },
      showPassword: false,
      rememberMe: false,
      loading: false
    }
  },
  computed: {
    canLogin() {
      return this.formData.username.trim() && this.formData.password.trim()
    }
  },
  onLoad() {
    this.initPage()
  },
  methods: {
    initPage() {
      // 检查是否已登录
      if (this.userStore.isLogin) {
        uni.switchTab({
          url: '/pages/index/index'
        })
        return
      }
      
      // 恢复记住的账号
      const savedAccount = uni.getStorageSync('savedAccount')
      if (savedAccount) {
        this.formData.username = savedAccount
        this.rememberMe = true
      }
    },
    
    togglePassword() {
      this.showPassword = !this.showPassword
    },
    
    toggleRemember() {
      this.rememberMe = !this.rememberMe
    },
    
    async handleLogin() {
      if (!this.validateForm()) return
      
      this.loading = true
      try {
        const loginData = {
          username: this.formData.username.trim(),
          password: this.formData.password.trim(),
        }
        
        const res = await loginAPI(loginData)
        
        // 登录成功判断 - 服务器返回数据包含message和token即为成功
        if (res.message && res.token) {
          // 登录成功
          this.userStore.login(res.user, res.token, res.refreshToken)
          
          // 记住账号
          if (this.rememberMe) {
            uni.setStorageSync('savedAccount', this.formData.username)
          } else {
            uni.removeStorageSync('savedAccount')
          }
          
          // 显示成功提示
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          })
          
          // 延迟跳转
          setTimeout(() => {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }, 1500)
        } else {
          // 登录失败
          uni.showToast({
            title: res.message || '登录失败，请重试',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('登录失败:', error)
        uni.showToast({
          title: error.message || '登录失败，请重试',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },
    
    validateForm() {
      if (!this.formData.username.trim()) {
        uni.showToast({
          title: '请输入用户名',
          icon: 'none'
        })
        return false
      }
      
      if (!this.formData.password.trim()) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return false
      }
      
      return true
    },
    
    goToForgotPassword() {
      uni.navigateTo({
        url: '/pages/password/forgot'
      })
    },
    
    goToRegister() {
      uni.navigateTo({
        url: '/pages/register/register'
      })
    }
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.bg-circle-1 {
  width: 300rpx;
  height: 300rpx;
  top: 10%;
  right: -100rpx;
  animation-delay: 0s;
}

.bg-circle-2 {
  width: 200rpx;
  height: 200rpx;
  top: 60%;
  left: -80rpx;
  animation-delay: 2s;
}

.bg-circle-3 {
  width: 150rpx;
  height: 150rpx;
  top: 80%;
  right: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

.login-container {
  position: relative;
  z-index: 1;
  padding: 40rpx 60rpx;
  min-height: calc(100vh - 88rpx);
}

.logo-section {
  text-align: center;
  margin: 80rpx 0 100rpx;
}

.logo {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 24rpx;
}

.app-name {
  display: block;
  font-size: 48rpx;
  font-weight: bold;
  color: #ffffff;
  margin: 32rpx 0 16rpx;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
}

.app-slogan {
  display: block;
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.8);
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 32rpx;
  padding: 60rpx 40rpx;
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  margin-bottom: 60rpx;
}

.login-type-tabs {
  display: flex;
  background-color: #f5f5f5;
  border-radius: 40rpx;
  padding: 6rpx;
  margin-bottom: 40rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 20rpx;
  border-radius: 34rpx;
  font-size: 28rpx;
  color: #666666;
  transition: all 0.3s;
}

.tab-item.active {
  background-color: #ffffff;
  color: #1890ff;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.input-group {
  margin-bottom: 32rpx;
}

.input-label {
  margin-bottom: 16rpx;
}

.input-label text {
  font-size: 28rpx;
  color: #333333;
  font-weight: 500;
}

.input-wrapper {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  border: 2rpx solid #e5e5e5;
  border-radius: 16rpx;
  padding: 0 24rpx;
  min-height: 88rpx;
  transition: all 0.3s;
}

.input-wrapper:focus-within {
  border-color: #1890ff;
  box-shadow: 0 0 0 4rpx rgba(24, 144, 255, 0.1);
}

.input-field {
  flex: 1;
  font-size: 28rpx;
  color: #333333;
  background-color: transparent;
  border: none;
  outline: none;
  height: 100%;
}

.input-placeholder {
  color: #999999;
}

.password-toggle {
  padding: 8rpx 16rpx;
  color: #1890ff;
  font-size: 24rpx;
}

.remember-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 40rpx 0;
}

.remember-item {
  display: flex;
  align-items: center;
}

.checkbox {
  width: 32rpx;
  height: 32rpx;
  border: 2rpx solid #d9d9d9;
  border-radius: 6rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.checkbox.checked {
  background-color: #1890ff;
  border-color: #1890ff;
}

.checkbox-mark {
  color: #ffffff;
  font-size: 20rpx;
  font-weight: bold;
}

.remember-text {
  font-size: 26rpx;
  color: #666666;
  margin-left: 16rpx;
}

.forgot-password {
  font-size: 26rpx;
  color: #1890ff;
}

.login-button-wrapper {
  margin: 40rpx 0;
}

.login-btn {
  width: 100%;
  height: 88rpx;
  background-color: #1890ff;
  color: #ffffff;
  border: none;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.login-btn.disabled {
  background-color: #d9d9d9;
  color: #ffffff;
}

.login-btn::after {
  border: none;
}

.register-link {
  text-align: center;
  margin-top: 32rpx;
}

.link-text {
  font-size: 28rpx;
  color: #666666;
  margin-right: 16rpx;
}

.link-btn {
  font-size: 28rpx;
  color: #1890ff;
}

/* 深色主题适配 */
@media (prefers-color-scheme: dark) {
  .login-page {
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
  }
  
  .app-name {
    color: #ffffff;
  }
  
  .app-slogan {
    color: rgba(255, 255, 255, 0.7);
  }
}
</style> 